﻿using MaterialManagement.API.Application.Command.MesWorkOrder;
using MaterialManagement.Domain.MesWorkOrder;
using MaterialManagement.ErrorCode;
using MaterialManagement.Infrastructure.Interface;
using MediatR;

namespace MaterialManagement.API.Application.Handler.MesWorkOrder
{
    public class GetBOMHandler : IRequestHandler<GetBOMCommand, APIPaging<BOMInformationModel>>
    {
        private readonly IBaseRepository<BOMInformationModel> bomInfoRepo;

        public GetBOMHandler(IBaseRepository<BOMInformationModel> bomInfoRepo)
        {
            this.bomInfoRepo = bomInfoRepo;
        }
        public Task<APIPaging<BOMInformationModel>> Handle(GetBOMCommand request, CancellationToken cancellationToken)
        {
            APIPaging<BOMInformationModel> result = new APIPaging<BOMInformationModel>();
            var res = bomInfoRepo.GetAll().Where(x => !x.IsDel).ToList();
            result.TotalCount = res.Count();
            result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.PageSize);
            result.PageDatas = res.OrderByDescending(x => x.Id).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            return Task.FromResult(result);
        }
    }
}
